package com.jichaoyun.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jichaoyun.common.model.Result;
import com.jichaoyun.model.entity.Valve;
import com.jichaoyun.model.entity.ValveAlarm;
import com.jichaoyun.model.entity.ValveChange;
import com.jichaoyun.model.vo.ValveComParametersVo;
import com.jichaoyun.service.ValveAlarmService;
import com.jichaoyun.service.ValveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = {"阀门接口列表"})
@RequestMapping("/sys/valve")
public class ValveController {
    @Autowired
    private ValveService valveService;
    @Autowired
    private ValveAlarmService valveAlarmService;

    @ApiOperation("阀门管理，新增阀门")
    @PostMapping("/addValve")
    public Result<?> addValve(@RequestBody Valve valve) {//@RequestBody是做json字符串的转换
        return valveService.addValve(valve);
    }

    @ApiOperation("阀门管理，删除阀门")
    @DeleteMapping("/deleteValve/{id}")
    public Result<Boolean> deleteValveById(@PathVariable("id") Integer id) {
        return Result.success(valveService.removeById(id));
    }

    @ApiOperation("阀门管理，更新阀门信息")
    @PostMapping("/updateValveInfo")
    public Result<?> updateValveInfo(@RequestBody Valve valve) {
        return valveService.updateValveInfo(valve);
    }

    @ApiOperation("阀门管理，查找所有阀门")
    @GetMapping("/getValveList")
    public Result<Map<String,Object>> getValveList(@RequestParam(value = "pageNo") Integer  pageNo){
        Map<String, Object> data= new HashMap<>();
        IPage<Valve> valveIPage = valveService.getValveList(pageNo,10);

        data.put("valveList",valveIPage.getRecords());
        data.put("pages",valveIPage.getPages());
        data.put("total",valveIPage.getTotal());
        return Result.success(data);
    }


    @ApiOperation("阀门管理，查找所有阀门开启程度修改记录")
    @GetMapping("/selectAllValveChangeData")
    public Result<Map<String,Object>> selectAllValveChangeData(@RequestParam(value = "pageNo") Integer  pageNo){
        Map<String, Object> data= new HashMap<>();
        IPage<ValveChange> valveIPage = valveService.selectAllValveChangeData(pageNo,5);

        data.put("valveChangeList",valveIPage.getRecords());
        data.put("pages",valveIPage.getPages());
        data.put("total",valveIPage.getTotal());
        return Result.success(data);
    }

    @ApiOperation("阀门控制，获取所有阀门名称")
    @GetMapping("/getValveNameList")
    public Result<List<String>> getValveNameList(){
        return Result.success(valveService.getAllValveName());
    }

    @ApiOperation("阀门控制，根据阀门名称获取阀门信息")
    @GetMapping("/getValveInfoByName")
    public Result<Map<String,Object>> getValveInfoByName(String valveName){
        return Result.success(valveService.getValveInfoByName(valveName));
    }

    @ApiOperation("阀门控制，根据阀门编号更新阀门开启程度")
    @PostMapping("/updateValveStatusById")
    public Result<?> updateValveStatusById(Integer valveId,byte status) throws IOException{
        return valveService.updateValveStatusById(valveId,status);
    }

    @ApiOperation("阀门控制，保存阀门修改程度信息")
    @PostMapping("/addValveChangeData")
    public Result<?> addValveChangeData(@RequestBody ValveChange valveChange) {
        return valveService.addValveChangeData(valveChange);
    }

    @ApiOperation("阀门报警，查找阀门报警信息")
    @GetMapping("/getValveAlarm")
    public Result<Map<String,Object>> getValveAlarm(@RequestParam(value = "pageNo") Integer  pageNo){
        Map<String, Object> data= new HashMap<>();
        IPage<ValveAlarm> valveAlarmIPage = valveAlarmService.getValveAlarm(pageNo,10);

        data.put("valveList",valveAlarmIPage.getRecords());
        data.put("pages",valveAlarmIPage.getPages());
        data.put("total",valveAlarmIPage.getTotal());
        return Result.success(data);
    }

    @ApiOperation(value = "阀门报警，阀门报警信息导出到excel")
    @GetMapping("/DownloadValveAlarmInfoPage")
    public void DownloadValveAlarmInfoPage(HttpServletResponse response) throws IOException {

        valveAlarmService.getValveAlarmExcel(response);

    }

    @ApiOperation(value = "阀门管理，阀门信息导出到excel")
    @GetMapping("/DownloadValveInfoPage")
    public void DownloadValveInfoPage(HttpServletResponse response) throws IOException {

        valveService.getValveExcel(response);

    }

    @ApiOperation(value = "阀门管理，修改阀门通讯参数")
    @PostMapping("/updateValveComParameters")
    public Result<Object> updateValveComParameters(ValveComParametersVo valveComParametersVo) {

        return valveService.updateComParameters(valveComParametersVo);
    }
}
